package com.bdilab.iot.utils;

import com.bdilab.iot.exception.ServerException;
import com.bdilab.iot.model.Permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangNing
 * @version 1.0
 * @date 2020/8/2 13:01
 */
public class ParsePermissionUtils {

    public static Map<String,Map<String, List<Permission>>> parsePermission(List<Permission> permissions){

        Map<String,Map<String, List<Permission>>> result = new HashMap<>();
        permissions.forEach(permission -> {
            String[] permissionTags = permission.getName().split(":");
            if(permissionTags.length != 3)
                throw new ServerException("权限名称不合法");
            if(!result.containsKey(permissionTags[0])){
                Map<String, List<Permission>> temp = new HashMap<>();
                result.put(permissionTags[0],temp);
            }
            Map<String, List<Permission>> item = result.get(permissionTags[0]);
            if(!item.containsKey(permissionTags[1])){
                List<Permission> permissionList = new ArrayList<>();
                item.put(permissionTags[1],permissionList);
            }
            List<Permission> permissionList = item.get(permissionTags[1]);
            permission.setName(permissionTags[2]);
            permissionList.add(permission);
        });
        return result;



    }

}
